# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms_hi.dm.dm_taking_timely_detail_hi import jms_dm__dm_taking_timely_detail_hi

label = f"dm_taking_timely_detail_hi_{int(time())}"
timeout = timedelta(minutes=60).seconds
doris_jms_dm__dm_taking_timely_detail_hi = RichSqlSensor(
    task_id='doris_jms_dm__dm_taking_timely_detail_hi',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_taking_timely_detail_hi PARTITION (
         p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+0) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+1) | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_detail_hi/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_detail_hi/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_detail_hi/dt={{{{ execution_date | date_add(+0) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_detail_hi/dt={{{{ execution_date | date_add(+1) | cst_ds }}}}/*"
        )
        INTO TABLE dm_taking_timely_detail_hi
        FORMAT AS 'PARQUET'(       
             order_no
            ,waybill_no
            ,order_source_code
            ,order_source_name
            ,config_id
            ,sender_province_code
            ,sender_province_name
            ,sender_city_code
            ,sender_city_name
            ,sender_area_code
            ,sender_area_name
            ,sender_network_code
            ,sender_network_name
            ,sender_fran_code
            ,sender_fran_name
            ,sender_agent_code
            ,sender_agent_name
            ,sender_region_code
            ,sender_region_name
            ,sender_staff_code
            ,sender_staff_name
            ,order_source_group_code
            ,order_source_group_name
            ,customer_code
            ,customer_name
            ,order_status_code
            ,pick_time
            ,pick_end_time
            ,cancel_time
            ,input_time
            ,store_send_time
            ,plate_flag
            ,taking_type
            ,if_tml_collect
            ,if_dispatch
            ,if_taking_miss
            ,if_taking_diff
            ,diff_code
            ,diff_name
            ,taking_time
            ,start_center_arrival_time
            ,weight_value
        )
        columns from path as (dt)
        set(
             order_no                   =  order_no
            ,waybill_no                 =  waybill_no
            ,order_source_code          =  order_source_code
            ,order_source_name          =  order_source_name
            ,config_id                  =  config_id
            ,sender_province_code       =  sender_province_code
            ,sender_province_name       =  sender_province_name
            ,sender_city_code           =  sender_city_code
            ,sender_city_name           =  sender_city_name
            ,sender_area_code           =  sender_area_code
            ,sender_area_name           =  sender_area_name
            ,sender_network_code        =  sender_network_code
            ,sender_network_name        =  sender_network_name
            ,sender_fran_code           =  sender_fran_code
            ,sender_fran_name           =  sender_fran_name
            ,sender_agent_code          =  sender_agent_code
            ,sender_agent_name          =  sender_agent_name
            ,sender_region_code         =  sender_region_code
            ,sender_region_name         =  sender_region_name
            ,sender_staff_code          =  sender_staff_code
            ,sender_staff_name          =  sender_staff_name
            ,order_source_group_code    =  order_source_group_code
            ,order_source_group_name    =  order_source_group_name
            ,customer_code              =  customer_code
            ,customer_name              =  customer_name
            ,order_status_code          =  order_status_code
            ,pick_time                  =  pick_time
            ,pick_end_time              =  pick_end_time
            ,cancel_time                =  cancel_time
            ,input_time                 =  input_time
            ,store_send_time            =  store_send_time
            ,plate_flag                 =  plate_flag
            ,taking_type                =  taking_type
            ,if_tml_collect             =  if_tml_collect
            ,if_dispatch                =  if_dispatch
            ,if_taking_miss             =  if_taking_miss
            ,if_taking_diff             =  if_taking_diff
            ,diff_code                  =  diff_code
            ,diff_name                  =  diff_name
            ,taking_time                =  taking_time
            ,start_center_arrival_time  =  start_center_arrival_time
            ,weight_value               =  weight_value
            ,dt                         =  dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dm__dm_taking_timely_detail_hi << jms_dm__dm_taking_timely_detail_hi
